/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.adama.util;

import fr.adama.constante.Constante;
import fr.adama.exception.ArchiveException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author mini-john
 */
public class ZipUtil {

    static int buffer = 2048;

    public static void zipBulletinPaie(File[] listFile, File outZip) throws FileNotFoundException, IOException {
        FileOutputStream fosZip = new FileOutputStream(outZip);
        try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(fosZip))) {
            byte data[] = new byte[buffer];
            for (File file : listFile) {
                FileInputStream fisTemp = new FileInputStream(file);
                try (BufferedInputStream origin = new BufferedInputStream(fisTemp, buffer)) {
                    ZipEntry entry = new ZipEntry(file.getName());
                    out.putNextEntry(entry);
                    int count;
                    while ((count = origin.read(data, 0, buffer)) != -1) {
                        out.write(data, 0, count);
                        out.flush();
                    }
                    out.closeEntry();
                }
            }

            out.flush();
        }
    }

    public static File getBulletinPDFFromZip(File zip) throws IOException, ArchiveException {
        File newFile;
        try (ZipFile zf = new ZipFile(zip)) {
            newFile = File.createTempFile(Constante.prefixBulletin, Constante.extensionPDF);
            Enumeration<? extends ZipEntry> entries = zf.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.getName().contains(Constante.extensionPDF) && entry.getName().contains(Constante.prefixBulletin)) {
                    try (InputStream inputStream = zf.getInputStream(entry); OutputStream output = new FileOutputStream(newFile)) {
                        copy(inputStream, output);
                        output.close();
                        inputStream.close();
                        return newFile;
                    }
                }

            }
        }
        throw new ArchiveException("Bulletin manquant dans l'archive",zip);
    }

    public static File getModelPaieXMLFromZip(File zip) throws IOException, ArchiveException {
        File newFile;
        try (ZipFile zf = new ZipFile(zip)) {
            newFile = File.createTempFile(Constante.prefixModelPaie, Constante.extensionXML);
            Enumeration<? extends ZipEntry> entries = zf.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.getName().contains(Constante.extensionXML) && entry.getName().contains(Constante.prefixModelPaie)) {
                    try (InputStream inputStream = zf.getInputStream(entry); OutputStream output = new FileOutputStream(newFile)) {
                        copy(inputStream, output);
                        output.close();
                        inputStream.close();
                         return newFile;
                    }
                }

            }
        }
           throw new ArchiveException("ModelPaie manquant dans l'archive",zip);
    }
    
    public static File getLigneHoraireXMLFromZip(File zip) throws IOException, ArchiveException {
        File newFile;
        try (ZipFile zf = new ZipFile(zip)) {
            newFile = File.createTempFile(Constante.prefixLigneHoraire, Constante.extensionXML);
            Enumeration<? extends ZipEntry> entries = zf.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.getName().contains(Constante.extensionXML) && entry.getName().contains(Constante.prefixLigneHoraire)) {
                    try (InputStream inputStream = zf.getInputStream(entry); OutputStream output = new FileOutputStream(newFile)) {
                        copy(inputStream, output);
                        output.close();
                        inputStream.close();
                         return newFile;
                    }
                }

            }
        }
           throw new ArchiveException("Ligne Horaire manquant dans l'archive",zip);
    }

    private static void copy(InputStream input, OutputStream output) throws IOException {
        byte[] bufferLocal = new byte[buffer];
        int size;
        while ((size = input.read(bufferLocal)) != -1) {
            output.write(bufferLocal, 0, size);
        }

    }
}
